/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.prashant.sqlduniya.dbexplorer;

import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;
import org.prashant.sqlduniya.sqlmanager.DBConnectionInfo;
import org.prashant.sqlduniya.sqlmanager.DBNode;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.prashant.sqlduniya.dbexplorer//DatabaseExplorer//EN",
autostore = false)
@TopComponent.Description(preferredID = "DatabaseExplorerTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = true)
@ActionID(category = "Window", id = "org.prashant.sqlduniya.dbexplorer.DatabaseExplorerTopComponent")
@ActionReference(path = "Menu/Window" /*
 * , position = 333
 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_DatabaseExplorerAction",
preferredID = "DatabaseExplorerTopComponent")
@Messages({
    "CTL_DatabaseExplorerAction=DatabaseExplorer",
    "CTL_DatabaseExplorerTopComponent=Database Explorer",
    "HINT_DatabaseExplorerTopComponent=This is a Database Explorer"
})
public final class DatabaseExplorerTopComponent extends TopComponent implements ExplorerManager.Provider {

    private final ExplorerManager manager = new ExplorerManager();

    public DatabaseExplorerTopComponent() {
        initComponents();
        setName(Bundle.CTL_DatabaseExplorerTopComponent());
        setToolTipText(Bundle.HINT_DatabaseExplorerTopComponent());

        
        initActions();

        associateLookup(ExplorerUtils.createLookup(manager, getActionMap()));

    }

    public void initTree(DBNode dbNode) {

//        Children children = new TableBeanNodeChildren(dbNode);
//        Node root = new AbstractNode(children);
        
        this.manager.setRootContext(
                new CatalogBeanNode("root", Children.create(
                new CatalogBeanFactory(dbNode), false)));
        ((BeanTreeView)jScrollPane1).setRootVisible(false);
    }

    private void initActions() {
//        CutAction cut = SystemAction.get(CutAction.class);
//        getActionMap().put(cut.getActionMapKey(), ExplorerUtils.actionCut(manager));
//        CopyAction copy = SystemAction.get(CopyAction.class);
//        getActionMap().put(copy.getActionMapKey(), ExplorerUtils.actionCopy(manager));
//        PasteAction paste = SystemAction.get(PasteAction.class);
//        getActionMap().put(paste.getActionMapKey(), ExplorerUtils.actionPaste(manager));
//        DeleteAction delete = SystemAction.get(DeleteAction.class);
//        getActionMap().put(delete.getActionMapKey(), ExplorerUtils.actionDelete(manager, true));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new BeanTreeView();

        setLayout(new java.awt.BorderLayout());
        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    @Override
    protected void componentActivated() {
        ExplorerUtils.activateActions(manager, true);
    }

    @Override
    protected void componentDeactivated() {
        ExplorerUtils.activateActions(manager, false);
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return manager;
    }
}
